package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.comment.dtos.CommentDto;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.dtos.WmSensitiveDto;
import com.heima.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/v1/sensitive")
public class SensitiveController {

    @Autowired
    private WmSensitiveService wmSensitiveService;

    /**
     * 敏感词 分页查询
     *
     * @param pageRequestDto 页码 每页显示条数
     * @return 当前页显示内容
     */
    @PostMapping("/list")
    public PageResponseResult list(@RequestBody PageRequestDto pageRequestDto) {
        Page page = wmSensitiveService.wmSensitivePage(pageRequestDto);
        return PageResponseResult.getPageResponseResult(page);
    }

    /**
     * 添加敏感词
     *
     * @param wmSensitiveDto 前端dto
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmSensitiveDto wmSensitiveDto) {
        wmSensitiveService.wmSensitiveSave(wmSensitiveDto);
        return ResponseResult.okResult(null);
    }

    /**
     * 修改敏感词
     *
     * @param wmSensitiveDto 前端dto
     * @return 响应结果
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmSensitiveDto wmSensitiveDto) {
        LambdaUpdateWrapper<WmSensitive> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(
                        wmSensitiveDto.getSensitives() != null
                                &&
                                StringUtils.isNotEmpty(wmSensitiveDto.getSensitives()),
                        WmSensitive::getSensitives,
                        wmSensitiveDto.getSensitives())
                .eq(wmSensitiveDto.getId() != null, WmSensitive::getId, wmSensitiveDto.getId());
        wmSensitiveService.update(updateWrapper);
        return ResponseResult.okResult(null);
    }

    /**
     * 根据id删除敏感词
     *
     * @param id 敏感词id
     * @return 响应结果
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult del(@PathVariable Integer id) {
        if (id == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmSensitiveService.removeById(id);
        return ResponseResult.okResult(null);
    }

    /**
     * 扫描当内容是否有敏感词存在
     * @param commentDto 文本内容
     * @return ture没有敏感词 false有敏感词
     */
    @PostMapping("/sensitiveScan")
    public ResponseResult<Boolean> scan(@RequestBody CommentDto commentDto){
        Boolean rs = wmSensitiveService.scan(commentDto);
        return ResponseResult.okResult(rs);
    }
}
